<template>
	<view class="backtheme">
		<!-- <header-top :messageInfo="{typeFlag:true,message:'授权登录'}"></header-top> -->
		<view class="loginpage">
			<view class="logoImg-box">
				<image src="../../static/image/system/login.png" class="logoImg"></image>
			</view>
			<view class="logintext">
				<view class="applytext">申请获取一下权限</view>
				<view class="obtaintext">获取你的公开信息（昵称、头像等）</view>
			</view>
			<view class="loginbtn">
				<button class="applybtn" @click="wxlogin">授权登录</button>
				<view class="temporarily-not-loginbtn" @click="closeLogin">暂不登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				third_id:"",
				code:""
			}
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			// this.wxlogin()
		},
		methods: {
			getPhoneNumber(e) {
				var _this = this;
				let version = uni.getSystemInfoSync().SDKVersion;
				console.log(e,99);
				if(e.detail.errMsg == "getPhoneNumber:ok"){
					// if(_this.compareVersion(version,'2.21.2')>=0){
					// 	_this.phoneNumber(_this,{code: e.detail.code})
					// }else{
						uni.login({
							provider: 'weixin',
							success: (auth) => {
								_this.phoneNumber(_this,{code: e.detail.code,iv: e.detail.iv,encryptedData:e.detail.encryptedData})
							},
							fail: (err) => {
								uni.showToast({'title':'微信登录授权失败',icon:"none"});
							}
						})
					// }
				}else{
				}
			},
			phoneNumber(that,data){
				let _this = this;
				data['third_id'] = this.third_id;
				httpRequest.request('/api/user/get_mobile', 'GET', data).then(resp => {
					if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else if (resp.code == 1) {
						uni.setStorageSync('token', resp['data']['userinfo']['token']);
						uni.setStorageSync('user_id', resp['data']['userinfo']['user_id']);
						uni.setStorageSync('invite_code', resp['data']['userinfo']['invite_code']);
						// 二维码绑定上下级关系开始
						// _this.bindGx();
						// 二维码绑定上下级关系结束
						httpRequest.toast("登录成功");
						setTimeout(() => {
							// let userinfo = resp['data']['userinfo'];
							// uni.setStorageSync('user_id', resp['data']['userinfo']['id']);
							uni.reLaunch({
								url:"/pages/index/index"
							})
						}, 1000)
					}
				}).catch(err=>{
				})
				
			},
			bindGx() {
				if (!httpRequest.checkIsLogin()) {
					return false;
				}
				if(!uni.getStorageSync('yqrinvite_code')){
					return false
				}
				httpRequest.request('/api/user/bind_relation', 'POST', {
					scene: uni.getStorageSync('yqrinvite_code')
				}, false, false, false).then(res => {
					if (res.code == 1) {
					} else if (res.code == 0) {
					} else {
					}
				}).catch(err => {
					// tui.bindGx();
				})
			},
			
			//版本比较
			compareVersion(v1, v2) {
			  v1 = v1.split('.')
			  v2 = v2.split('.')
			  const len = Math.max(v1.length, v2.length)
			
			  while (v1.length < len) {
			    v1.push('0')
			  }
			  while (v2.length < len) {
			    v2.push('0')
			  }
			
			  for (let i = 0; i < len; i++) {
			    const num1 = parseInt(v1[i])
			    const num2 = parseInt(v2[i])
			
			    if (num1 > num2) {
			      return 1
			    } else if (num1 < num2) {
			      return -1
			    }
			  }
			  return 0
			},
			wxlogin() {
				let _this = this
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				uni.getUserInfo({
					success(userres) {
						console.log(userres);
						uni.login({
							success(res) {
								if (res.code) {
									// _this.code = res.code
									// uni.setStorageSync('code', res.code);
									httpRequest.request('/api/user/wxlogin', 'GET', {
										code:res.code
									}).then(resp => {
										uni.hideLoading()
										if(resp.data.third.binding==1){
											uni.setStorageSync('token', resp['data']['userinfo']['token']);
											uni.setStorageSync('user_id', resp['data']['userinfo']['user_id']);
											
											httpRequest.toast("登录成功");
											setTimeout(() => {
												uni.reLaunch({
													url:"/pages/index/index"
												})
											}, 1000)
										}else{
											uni.navigateTo({
												url:"/pages/login/wanshanxx?id="+resp.data.third.third_id
											})
										}
									}).catch(err=>{
									})
								}
							},
							fail(err) {
								uni.hideLoading()
							}
						})
					},
					fail(err) {
						uni.hideLoading()
					}
				})
				
			},
			// 暂时不登陆
			closeLogin() {
				uni.navigateBack(1)
			}

		}
	}
</script>

<style>
	page{
		background-color: #fff;
	}
	button {
	  line-height: 80rpx;
	}
	.loginpage {
		padding: 158rpx 45rpx 0 45rpx;
	}

	.logoImg-box {
		text-align: center;
		border-bottom: 1rpx solid #B8B8B8;
		width: 100%;
		padding-bottom: 42rpx;
	}

	.logoImg {
		width: 216rpx;
		height: 216rpx;
	}

	.applytext {
		color: #333333;
		font-size: 34rpx;
		line-height: 40rpx;
		margin-top: 59rpx;
		font-weight: bold;
	}

	.obtaintext {
		color: #666666;
		font-size: 28rpx;
		line-height: 33rpx;
		margin-top: 25rpx;
		font-weight: bold;
	}

	.loginbtn {
		padding: 0 32rpx;
		color: #fff;
		font-size: 36rpx;
		line-height: 24rpx;
		font-weight: bold;
	}

	.applybtn {
		width: 100%;
		border-radius: 50rpx;
		background-color: #00ac8f;
		margin-top: 176rpx;
		height: 90rpx;
		color: #fff;
		text-align: center;
	}

	.temporarily-not-loginbtn {
		width: 100%;
		border-radius: 50rpx;
		background-color: #C5C5C5;
		margin-top: 20rpx;
		padding: 33rpx 0;
		text-align: center;
	}
</style>
